//
//  ViewController.m
//  TestCollectionView
//
//  Created by Iori on 08/09/2017.
//  Copyright © 2017 Iori. All rights reserved.
//

#import "ChooseHotSpotViewController.h"
#import "HotSpotEntity.h"
#import "HotSpotCollectionViewCell.h"
#import "GetHotPointApi.h"
#import "Header.h"

@interface ChooseHotSpotViewController () <UICollectionViewDataSource, UICollectionViewDelegate>
@property (strong, nonatomic) NSArray<HotSpotEntity*> *dataSource;
@property (strong, nonatomic) NSArray<HotSpotEntity*> *searchDataSource;
@property (strong, nonatomic) NSMutableArray<HotSpotEntity*> *selectedDataSource;
@property (assign, nonatomic) BOOL isSearching;
@property (nonatomic, copy) NSString *selectedHotID;

@end

@implementation ChooseHotSpotViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.selectedDataSource = [NSMutableArray arrayWithCapacity:4];
    self.view.backgroundColor = [UIColor colorWithHexString:@"f8f8f8"];
    self.selectedHotID = [self.chooseHotSpotDelegate getChooseHotID];
    WS(weakSelf);
    [self.collectionView addPullToRefreshWithActionHandler:^{
        GetHotPointApi *api = [GetHotPointApi new];
        api.admin_id = [UserInfo sharedUser].admin.admin_id;
        
        [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request)
         {
             [weakSelf.collectionView.pullToRefreshView stopAnimating];
             if([IoriJsonHelper getErrorCode:request] == 100)
             {
                 weakSelf.dataSource = [NSArray yy_modelArrayWithClass:[HotSpotEntity class] json:[IoriJsonHelper getDictForKey:@"hot" fromDict:request.responseJSONObject]];
                 [weakSelf.collectionView reloadData];
             }
             else
             {
                 
             }
         } failure:^(YTKBaseRequest *request) {
             [weakSelf.collectionView.pullToRefreshView stopAnimating];
         }];
    }];
    
    [weakSelf.collectionView triggerPullToRefresh];
//    NSMutableArray<HotSpotEntity*> *array = [NSMutableArray arrayWithCapacity:4];
//    HotSpotEntity *entity = [HotSpotEntity new];
//    entity.name = @"乐视 贾跃亭";
//    entity.value = @"1";
//    [array addObject:entity];
//    entity = [HotSpotEntity new];
//    entity.name = @"马云 无人超市";
//    entity.value = @"2";
//    [array addObject:entity];
//    entity = [HotSpotEntity new];
//    entity.name = @"中印边境对峙";
//    entity.value = @"3";
//    [array addObject:entity];
//    self.dataSource = array;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - collectionview delegate -

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if(self.isSearching)
    {
        return self.searchDataSource.count;
    }
    return self.dataSource.count;
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HotSpotCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.layer.borderWidth = 1;
    cell.layer.cornerRadius = 4;
    cell.layer.borderColor = [UIColor colorWithHexString:@"#eeeeee"].CGColor;
    cell.backgroundColor = [UIColor clearColor];
    if(self.isSearching)
    {
        cell.labTitle.text = self.searchDataSource[indexPath.item].name;
        if([self.selectedDataSource containsObject:self.searchDataSource[indexPath.item]])
        {
            cell.backgroundColor = [UIColor colorWithHexString:@"#eeeeee"];
            cell.layer.borderColor = [UIColor colorWithHexString:@"#eeeeee"].CGColor;
        }
    }
    else
    {
        if([self.selectedDataSource containsObject:self.dataSource[indexPath.item]])
        {
            cell.backgroundColor = [UIColor colorWithHexString:@"#eeeeee"];
            cell.layer.borderColor = [UIColor colorWithHexString:@"#eeeeee"].CGColor;
        }
        if([self.dataSource[indexPath.item].value isEqualToString:self.selectedHotID])
        {
            cell.backgroundColor = [UIColor colorWithHexString:@"#eeeeee"];
            cell.layer.borderColor = [UIColor colorWithHexString:@"#eeeeee"].CGColor;
        }
        cell.labTitle.text = self.dataSource[indexPath.item].name;
    }
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self.selectedDataSource removeAllObjects];
    if(self.isSearching)
    {
        if([self.selectedDataSource containsObject:self.searchDataSource[indexPath.item]])
        {
            [self.selectedDataSource removeObject:self.searchDataSource[indexPath.item]];
        }
        else
        {
            [self.selectedDataSource addObject:self.searchDataSource[indexPath.item]];
        }
    }
    else
    {
        self.selectedHotID = nil;
        if([self.selectedDataSource containsObject:self.dataSource[indexPath.item]])
        {
            [self.selectedDataSource removeObject:self.dataSource[indexPath.item]];
        }
        else
        {
            [self.selectedDataSource addObject:self.dataSource[indexPath.item]];
        }
    }
    //[collectionView reloadItemsAtIndexPaths:@[indexPath]];
    [collectionView reloadData];
}

#pragma mark - searchbar delegate -

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    if([[searchText stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] isEqualToString:@""])
    {
        self.selectedDataSource = [NSMutableArray arrayWithCapacity:4];
        self.isSearching = NO;
        [self.collectionView reloadData];
    }
    else
    {
        self.selectedDataSource = [NSMutableArray arrayWithCapacity:4];
        self.isSearching = YES;
        NSMutableArray<HotSpotEntity*> *tempArray = [NSMutableArray arrayWithCapacity:4];
        [self.dataSource enumerateObjectsUsingBlock:^(HotSpotEntity * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if([obj.name containsString:self.searchBar.text])
            {
                [tempArray addObject:obj];
            }
        }];
        self.searchDataSource = tempArray;
        [self.collectionView reloadData];
    }
}

#pragma mark - action -
- (IBAction)btnOk_click:(UIButton *)sender
{
    if([self.chooseHotSpotDelegate respondsToSelector:@selector(chooseHotSpot:)])
    {
        __weak __typeof(&*self)weakSelf = self;

        if(self.isSearching)
        {
            [self.chooseHotSpotDelegate chooseHotSpot:self.selectedDataSource];
        }
        else
        {
            [self.chooseHotSpotDelegate chooseHotSpot:self.selectedDataSource];
        }
        
    }
    [self.navigationController popViewControllerAnimated:YES];
}

@end
